<?php
	
	function getForm($error = '', $user = '')
	{
		echo '
			Welcome N0oB!<br>
			Please register or login.<br>
			<form method="POST" action="index.php?login" name="mlglogin" autocomplete="off">
			<table>

				<tr><td width="100">Username:</td><td><input type="text" name="username" value="'.$user.'" placeholder="Username"></td></tr>
				<tr><td width="100">Password:<ordtd><td><input type="password" name="password" placeholder="Password" autocomplete="off"></td></tr>
				<tr><td></td><td><input type="submit" value="Login"> &nbsp;&nbsp; <a href="?page=pwrecover">Password recovery</a></td></tr>
				
			
			</form>
			</table>

				<button class="btnregister">Register</button>
				<tr><td colspan="2"><font color="red">'.$error.'</font></td></tr>';
	}

	function getLogin()
	{
			//login
			if($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_GET['login']))
			{
				$username = $_POST['username'];
				$password = $_POST['password'];
				// encrypt password
				$password = encrypt($password);

				//no username or password filled in.
				if(empty($username) || empty($password))
				{
					$error = '- Please fill in all fields.';
				}

				if(isset($error))
				{
					getForm($error = $error, $user = $username);
				}
				else
				{
					$q = mysql_query("SELECT * FROM account WHERE username='".$username."'");
					if(mysql_num_rows($q) == 0)
					{
						$error = '- Username doesn\'t exist.';
					}

					if(isset($error))
					{
						getForm($error = $error, $user = $username);
					}
					else
					{
						$q = mysql_query("SELECT * FROM account WHERE username='".$username."' AND password='".$password."'");
						if(mysql_num_rows($q) == 0)
						{
							$error = '- Wrong password.';
						}

						if(isset($error))
						{
							getForm($error = $error, $user = $username);
						}
						else
						{
							$q = mysql_query("SELECT id, grade, username FROM account WHERE username='".$username."'");
							$f = mysql_fetch_array($q);

							if($f['grade'] == 0)
							{
								$error = '- Please check your email for an activation link.';
								getForm($error = $error, $user = $username);
							}
							else
							{
								$_SESSION['uid'] = $f['id'];
								$_SESSION['user'] = $f['username'];

								$q = mysql_query("SELECT id, name FROM characters WHERE aid='".$f['id']."'");
								$f = mysql_fetch_array($q);
								$_SESSION['cid'] = $f['id'];
								$_SESSION['char'] = $f['name'];

								$_SESSION['ip'] = $_SERVER['REMOTE_ADDR'];
								redirect('?page=profile');
							}
							
						}
					}
				}
			}
			else
			{
				getForm();
			}
	}


	function getAccInfo()
	{
		if(isset($_GET['logout']))
		{
			session_destroy();
			redirect('index.php');
		}

		if(!isset($_SESSION['audioplayed']))
		{
			echo '<audio controls autoplay style="position:fixed; left:0; top:0; width:0; height:0; opacity:0;">
				  <source src="js/login.ogg" type="audio/ogg">
				  Your browser does not support the audio element.
				</audio>';
			$_SESSION['audioplayed'] = '1';
		}

		$id = $_SESSION['uid'];
		
		echo 'Welcome '.$_SESSION['user'].' - <a href="?page=messages">Messages ( '.getUnreadMessages().' )</a> - <a href="?logout">Logout</a><br>';

		echo '
		<div class="imgname">
			<a href="?page=profile"><img src="img/characters/'.getCharpic($id).'"" class="charpic"></a>
			<div class="name">'.getCharName($id).'</div>
		</div>';
		
		echo '<table style="stattable">';
		//info
		echo '<tr><td>Level:</td><td>'.getCharLevel($id).'</td></tr>';
		//bars
		echo'
		<tr><td width="100">HP:</td>
		<td><div class="statbar">
				<div class="backbar hp" style="width: '.getCharHPBar($id).'%;"></div>
				<div class="mainbar">'.getCharHPBar($id).'% ('.getCharCurrHP($id).'/'.getCharMaxHP($id).')</div>
			</div></td></tr>
		<tr><td>Energy:</td>
		<td><div class="statbar">
			<div class="backbar energy" style="width:  '.getCharEnergyBar($id).'%;"></div>
			<div class="mainbar">'.getCharEnergyBar($id).'% ('.getCharCurrEnergy($id).'/'.getCharMaxEnergy($id).')</div>
		</div></td></tr>
		<tr><td>XP Till lvl:</td>
		<td><div class="statbar">
			<div class="backbar xp" style="width: '.getCharXPBar($id).'%;"></div>
			<div class="mainbar">'.getCharXPBar($id).'% ('.getCharCurrXP($id, 1).'/'.getCharNeedXP($id).')</div>
		</div></td></tr>';



		echo '<tr><td>Pollars:</td><td>&#x2766;'.getPollars($id).'</td></tr>
				<tr><td>Pank:</td><td>&#x2766;'.getPankPollars($id).'</td></tr>
			<tr><td>Status:</td><td>'.getStatus($id).'</td></tr>';



		echo '</table>';


	}
?>